/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmAnggotaView.java
 *
 * Created on 20 Sep 12, 11:07:52
 */
package sistemperpustakaan.view.anggota;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import sistemperpustakaan.controller.AnggotaController;
import sistemperpustakaan.model.AnggotaModel;
import sistemperpustakaan.tablemodel.AnggotaTableModel;
import sistemperpustakaan.utility.ReportUtility;
import sistemperpustakaan.view.FrmMain;

/**
 *
 * @author Eko Aprianto
 */
public class FrmAnggotaView extends javax.swing.JInternalFrame implements InternalFrameListener, ListSelectionListener {

    public FrmMain frameMain;
    AnggotaModel model;
    AnggotaController controller;
    AnggotaTableModel tableModel;
    DialogAnggotaDetail anggotaDetail;
    int anggotaId = -1;

    /** Creates new form FrmAnggotaView */
    public FrmAnggotaView(FrmMain main) {
        this.frameMain = main;
        model = new AnggotaModel();
        controller = new AnggotaController(this, model);
        initComponents();
        tableAnggota.setModel(new AnggotaTableModel());
        tableAnggota.getSelectionModel().addListSelectionListener(this);
        addInternalFrameListener(this);
        setTableView();
        setCombo();
//        controller.findListAnggota();
    }

    protected final void setCombo() {
        controller.fillComboType(comboTypeAnggota);
        comboTypeAnggota.setSelectedIndex(0);

    }

    public final void setTableView() {
        int[] width = {80, 150, 150, 200, 150, 100, 300, 150, 150};
        for (int i = 0; i < 9; i++) {
            tableAnggota.getColumnModel().getColumn(i).setPreferredWidth(width[i]);
        }
        centerCellRenderer.setHorizontalAlignment(JLabel.CENTER);
        rightCellRenderer.setHorizontalAlignment(JLabel.RIGHT);
        tableAnggota.getColumnModel().getColumn(5).setCellRenderer(centerCellRenderer);
        tableAnggota.getColumnModel().getColumn(6).setCellRenderer(centerCellRenderer);
        tableAnggota.getColumnModel().getColumn(8).setCellRenderer(centerCellRenderer);
    }
    DefaultTableCellRenderer centerCellRenderer = new DefaultTableCellRenderer();
    DefaultTableCellRenderer rightCellRenderer = new DefaultTableCellRenderer();

    public JComboBox getComboTypeAnggota() {
        return comboTypeAnggota;
    }

    public void setComboTypeAnggota(JComboBox comboTypeAnggota) {
        this.comboTypeAnggota = comboTypeAnggota;
    }

    public JTable getTableAnggota() {
        return tableAnggota;
    }

    public void setTableAnggota(JTable tableAnggota) {
        this.tableAnggota = tableAnggota;
    }

    public JTextField getTxtId() {
        return txtId;
    }

    public void setTxtId(JTextField txtId) {
        this.txtId = txtId;
    }

    public JTextField getTxtNama() {
        return txtNama;
    }

    public void setTxtNama(JTextField txtNama) {
        this.txtNama = txtNama;
    }

    public JTextField getTxtNis() {
        return txtNis;
    }

    public void setTxtNis(JTextField txtNis) {
        this.txtNis = txtNis;
    }

    public int getAnggotaId() {
        return anggotaId;
    }

    public void setAnggotaId(int anggotaId) {
        this.anggotaId = anggotaId;
    }

    public DialogAnggotaDetail getAnggotaDetail() {
        return anggotaDetail;
    }

    public void setAnggotaDetail(DialogAnggotaDetail anggotaDetail) {
        this.anggotaDetail = anggotaDetail;
    }

    public AnggotaController getController() {
        return controller;
    }

    public void setController(AnggotaController controller) {
        this.controller = controller;
    }

    public AnggotaModel getModel() {
        return model;
    }

    public void setModel(AnggotaModel model) {
        this.model = model;
    }

    public AnggotaTableModel getTableModel() {
        return tableModel;
    }

    public void setTableModel(AnggotaTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public JLabel getLblJumlah() {
        return lblJumlah;
    }

    public void setLblJumlah(JLabel lblJumlah) {
        this.lblJumlah = lblJumlah;
    }

    public void setAction() {
        frameMain.getBtnCari().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.findListAnggota();
                }
            }
        });

        frameMain.getBtnRefresh().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    controller.refreshListAnggota();
                }
            }
        });

        frameMain.getBtnDetail().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (anggotaId > -1 && isSelected()) {
                    anggotaDetail = new DialogAnggotaDetail(frameMain, true, controller, anggotaId);
                    anggotaDetail.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");
                }
            }
        });

        frameMain.getBtnTambah().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected()) {
                    anggotaDetail = new DialogAnggotaDetail(frameMain, true, controller);
                    anggotaDetail.setVisible(true);
                }
            }
        });

        frameMain.getBtnHapus().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (anggotaId > -1 && isSelected()) {
                    controller.saveDeleteAnggota();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Anda belum memilih data");
                }
            }
        });
        frameMain.getBtnViewReport().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected() && tableAnggota.getRowCount() > 0) {

                    controller.report(ReportUtility.SHOW);
                }
            }
        });

        frameMain.getBtnPrintReport().setActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isSelected() && tableAnggota.getRowCount() > 0) {
                    controller.report(ReportUtility.PRINT);
                }
            }
        });
    }

    public int cekId() {
        int id;
        if (txtId.getText().isEmpty()) {
            id = -1;
        } else {
            id = Integer.parseInt(txtId.getText());
        }
        return id;
    }

    public String cekNis() {
        String nis;
        if (txtNis.getText().isEmpty()) {
            nis = "_";
        } else {
            nis = txtNis.getText().toString();
        }
        return nis;
    }
    public String cekNama() {
        String nama;
        if (txtNama.getText().isEmpty()) {
            nama = "_";
        } else {
            nama = txtNama.getText().toString();
        }
        return nama;
    }

    public String cekType() {
        String type;
        if (comboTypeAnggota.getSelectedIndex() == 1) {
            type = "Siswa";
        } else if (comboTypeAnggota.getSelectedIndex() == 2) {
            type = "G";
        } else {
            type = "_";
        }
        return type;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableAnggota = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNis = new javax.swing.JTextField();
        txtNama = new javax.swing.JTextField();
        comboTypeAnggota = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        lblJumlah = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        gbr = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Data Anggota");

        tableAnggota.setAutoCreateRowSorter(true);
        tableAnggota.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableAnggota.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tableAnggota);

        jLabel1.setText("NIS / NIK");

        jLabel2.setText("Nama");

        comboTypeAnggota.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Type Anggota");

        jLabel4.setText("ID");

        lblJumlah.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblJumlah.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblJumlah.setText("0");

        jLabel5.setText("item(s) found");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel6.setText("Data Anggota perpustakaan");

        gbr.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sistemperpustakaan/image/anggota.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtNis, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboTypeAnggota, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gbr)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(728, Short.MAX_VALUE)
                .addComponent(lblJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(22, 22, 22))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 858, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addComponent(txtNis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(7, 7, 7)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1)))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboTypeAnggota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(10, 10, 10))
                    .addComponent(gbr, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel5))
                    .addComponent(lblJumlah))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboTypeAnggota;
    private javax.swing.JLabel gbr;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblJumlah;
    private javax.swing.JTable tableAnggota;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtNama;
    private javax.swing.JTextField txtNis;
    // End of variables declaration//GEN-END:variables

    public void internalFrameOpened(InternalFrameEvent e) {
    }

    public void internalFrameClosing(InternalFrameEvent e) {
    }

    public void internalFrameClosed(InternalFrameEvent e) {
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    }

    public void internalFrameActivated(InternalFrameEvent e) {
        setAction();
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
    }

    public void valueChanged(ListSelectionEvent e) {
        try {
            anggotaId = Integer.valueOf(tableAnggota.getValueAt(tableAnggota.getSelectedRow(), 0).toString());
        } catch (Exception ex) {
        }
    }
}
